package com.trkj.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trkj.wiki.mapper.CategoryMapper;
import com.trkj.wiki.pojo.Category;
import com.trkj.wiki.pojo.CategoryExample;
import com.trkj.wiki.req.CategoryQueryReq;
import com.trkj.wiki.req.CategorySaveReq;
import com.trkj.wiki.resp.CategoryQueryResp;
import com.trkj.wiki.resp.PageResp;
import com.trkj.wiki.util.CopyUtil;
import com.trkj.wiki.util.SnowFlake;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Log4j2
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Resource
    private SnowFlake snowFlake;

    public List<CategoryQueryResp> all (){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        List<CategoryQueryResp> respList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        return respList;
    }

    public PageResp<CategoryQueryResp> list(CategoryQueryReq req){
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        categoryExample.setOrderByClause("sort asc");
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        PageInfo<Category> pageInfo=new PageInfo<>(categoryList);
        log.info("总行数：{}",pageInfo.getTotal());
        log.info("总页数：{}",pageInfo.getPages());

    /*    List<CategoryResp> respList=new ArrayList<>();
        for (Category category : categoryList) {
            CategoryResp categoryResp = new CategoryResp();
            BeanUtils.copyProperties(category,categoryResp);
            respList.add(categoryResp);
        }*/

        List<CategoryQueryResp> respList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);


        return pageResp;
    }

    /**
     * @Author Twx
     * @Date 2021/4/25 14:34
     * @Description 电子书新增/修改
     * @Param [req]
     * @Return void
     * @Since version-1.0
     */
    public void save(CategorySaveReq req){
        Category category=CopyUtil.copy(req,Category.class);
        if(ObjectUtils.isEmpty(req.getId())){
            //新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }else {
            //更新
            categoryMapper.updateByPrimaryKey(category);
        }

    }

    /*
     * @Author Twx
     * @Date 2021/4/25 20:28
     * @Description 删除电子书
     * @Param [id]
     * @Return void
     * @Since version-1.0
     */
    public void delete(long id){
        categoryMapper.deleteByPrimaryKey(id);
    }

}
